package com.cordova.test.imageselect.view

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.cordova.test.imageselect.R

class ImagePreviewActivity : AppCompatActivity() {

    companion object {
        private const val EXTRA_IMAGE_PATH: String = "image_path"
        fun start(context: Context, path: String?) {
            val intent = Intent(context, ImagePreviewActivity::class.java)
            intent.putExtra(EXTRA_IMAGE_PATH, path)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image_preview)
        toolBar()
        initView()
    }

    private fun initView() {
        val imagePath: String = intent.getStringExtra(EXTRA_IMAGE_PATH)
        var previewImage: ImageView = findViewById(R.id.img_preview) as ImageView
        Glide.with(this@ImagePreviewActivity).load(imagePath).error(R.mipmap.v).into(previewImage)
    }

    /**
     * toolBar 配置
     */
    private fun toolBar() {
        val toolBar: Toolbar = findViewById(R.id.img_preview_toolbar) as Toolbar
        // 主标题
        toolBar.title = ""
        setSupportActionBar(toolBar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
//        supportActionBar?.setHomeButtonEnabled(true)
//        左边的小箭头（注意需要在setSupportActionBar(toolbar)之后才有效果）
        toolBar.setNavigationIcon(R.drawable.ic_arrow_back)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == android.R.id.home) {
            this.finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }
}
